<?php 
	class Gift extends CI_Controller{
		public function __construct(){
			parent::__construct();
			$this->load->library("email");
		}
		
		public function register($str=""){
			//get customer id
			if($str == "chalachol"){
				$club_id = $this->input->post("club_id");
				if($club_id !=""){
					$count_id = $this->check_club_id($club_id);
					if($count_id == 1){
						$ship = "1";
					}else {
						$ship = "0";
					}		
				}else{
					$ship = "0";
				}
			}else{
				$club_id = "";
				 $ship = "0";
			}
			$this->db->select("pkid,name,email");
			$this->db->where("name",$str);
			$row 			= $this->db->get("mobile_customer")->row();
			$email_to		= $row->email;
			$c_pkid 			= $row->pkid;
			
			$name 		=$this->input->post('name');
			$lastname	=$this->input->post("lastname");
			$phone 		=$this->input->post('tel');
			$email 		=$this->input->post('email');
			$rec 			=$this->input->post('rec');
			$imie			=$this->input->post("imie");
			$sex			=$this->input->post("sex");
			if($sex != ""){
				if($sex == "M"){
					$sex = 0;
				}else{
					$sex = 1;
				}
			}
			if($rec != ""){
				if($phone == $rec){
					$point = 6;
				}else {
					$this->db->where("tel",$rec);
					if($this->db->count_all_results("mobile_member_gift") > 0){
						$this->db->select("pkid,point");
						$this->db->where("tel",$rec);
						$this->db->where("customer_pkid",$c_pkid);
						$this->db->limit(1);
						$rows =$this->db->get("mobile_member_gift")->row();
						$point =$rows->point+5;
						//update
						$this->db->where("pkid",$rows->pkid);
						$this->db->update("mobile_member_gift",array("point"=>$point));	
						$point = 1;		
					}else{
						$point = 1;
					}
				}
			}else{
				$point = 1;
			}
			
				try {
					$this->db->where("imei",$imie);
					$this->db->where("customer_pkid",$c_pkid);
					$num_imei = $this->db->count_all_results("mobile_member_gift");
					if($num_imei == 0){
					$data = array(
								"name"=>$name,
								"lastname"=>$lastname,
								"sex"=>$sex,
								"email"=>$email,
								"tel"=>$phone,
								"id_pc"=>$rec,
								"imei"=>$imie,
								"point"=>$point,
								"customer_pkid"=>$c_pkid,
								"member_club_id"=>$club_id,
								"member_type"=>$ship
							);
					$this->db->insert("mobile_member_gift",$data);
					$lastid = $this->db->insert_id();
					$this->db->insert("mobile_member_gift_log",array("member_pkid"=>$lastid,"login_date"=>date('Y-m-d')));
					}
					$arr['StatusID']  = "1";
					$arr['Error']		="";
				}catch (Exception $e) {
					$arr['StatusID']  = "0";
					$arr['Error']		="Send Email Fail";
				}
			echo json_encode($arr);
		}
		
		private function check_club_id($id){
			$this->db->where("MemberID",$id);
			$num =$this->db->count_all_results("mobile_chalachol_member");
			if($num > 0){ //found
				return 1;
			}else{
				return 0;
			}
		}
		
		public function check_imie(){
			$imie = $this->input->post("imie");  //paramitor
			$c_name = $this->input->post("customer_name");
			//===
			$this->db->where("name",$c_name);
			$row_customer = $this->db->get("mobile_customer")->row();
			// select * from mobile_customer where name="praramitor $c_name";
			//===
			if($imie != ""){
				$this->db->where("imei",$imie);
				$this->db->where("customer_pkid",$row_customer->pkid);
				$num = $this->db->count_all_results("mobile_member_gift");
			}
			if($num == 0){
				$arrs['StatusID']  	= "1";
				$arrs['Error']			="Not found imie";
				$arrs['Membertype']		="";
			}else{
				//insert log login
				$this->db->select("pkid,point,member_type");
				$this->db->where("imei",$imie);
				$this->db->where("customer_pkid",$row_customer->pkid);
				$rowm = $this->db->get("mobile_member_gift")->row();
				// check log
				$this->db->where("member_pkid",$rowm->pkid);
				$this->db->where("login_date",date('Y-m-d'));
				if($this->db->count_all_results("mobile_member_gift_log") == 0){
					//update point 
					$point = $rowm->point;
					$point += 1;
					$this->db->where("pkid",$rowm->pkid);
					$this->db->update("mobile_member_gift",array("point"=>$point));
					
					// insert log
					$this->db->insert("mobile_member_gift_log",array("member_pkid"=>$rowm->pkid,"login_date"=>date('Y-m-d')));
				}
				
				$arrs['StatusID']  	= "0";
				$arrs['Error']			="";
				$arrs['Membertype']=$rowm->member_type;
			}
			echo json_encode($arrs);
		}
		
		public function get_gift($imei=""){
			//$imei = $this->input->post("imei");
			//$imei = "358867040228009";
			$arr = array();
			$this->db->select("pkid,name,point");
			$this->db->where("imei",$imei);
			$this->db->where("customer_pkid",12);
			$rowmember =$this->db->get("mobile_member_gift")->row();
			$tmp['mname']			=$rowmember->name;
			$tmp['point']				=$rowmember->point;
			// get gift
			$this->db->where("status",1);
			$this->db->where("customer_pkid",12);
			$this->db->order_by("pkid","desc");
			$rowsgift =$this->db->get("mobile_gift")->row();
			if(count($rowsgift) > 0){
				$tmp['gname']				=$rowsgift->name;
				$tmp['content']				=$rowsgift->content;
				$tmp['img']					=$rowsgift->img;
				// check code 
				$this->db->where("member_pkid",$rowmember->pkid);
				$this->db->where("gift_pkid",$rowsgift->pkid); 
				if($this->db->count_all_results("mobile_code_gift") > 0){
					$this->db->select("code");
					$this->db->where("member_pkid",$rowmember->pkid);
					$this->db->where("gift_pkid",$rowsgift->pkid);
					$rowc = $this->db->get("mobile_code_gift")->row();
					$tmp['status'] = 'n';
					$tmp['codegen'] = $rowc->code;
				}else{
					$tmp['status'] = 'y';
					$tmp['codegen']="";
				}
			}else{
				$tmp['gname']				="";
				$tmp['content']				="";
				$tmp['img']					="";
				$tmp['status'] ="";
				$tmp['codegen'] ="";
			}
			
			$arr[] = $tmp;
			echo json_encode($arr);
		}
		
		public function gen_code(){
			$rm			=$this->input->post("rm");
			$imei			=$this->input->post("imei");
			$this->db->select("pkid");
			$this->db->where("status",1);
			$this->db->where("customer_pkid",12);
			$this->db->order_by("pkid","desc");
			$rowsgift =$this->db->get("mobile_gift")->row();
			if($rm != "" && $imei != ""){
				$this->db->select("pkid");
				$this->db->where("imei",$imei);
				$this->db->where("customer_pkid",12);
				$rowm = $this->db->get("mobile_member_gift")->row();
				$data = array(
							"member_pkid"=>$rowm->pkid,
							"gift_pkid"=>$rowsgift->pkid,
							"code"=>$rm,
							"created_date"=>date('Y-m-d H:i:s')
						);
				$res = $this->db->insert("mobile_code_gift",$data);
				if($res){
					$arrs['StatusID']  	= "1";
					$arrs['Error']			="Success";
				}else{
					$arrs['StatusID']  	= "0";
					$arrs['Error']			="Fail";
				}
			}else{
					$arrs['StatusID']  	= "0";
					$arrs['Error']			="Fail";
			}
			echo json_encode($arrs);
		}
		
		public function history_gift($imei="",$str=""){
			$arrs = array();
			//$imei = "358867040228009";
			if($str != "sherman"){
				$this->db->where("name",$str);
				$row_customer = $this->db->get("mobile_customer")->row();
				$cid = $row_customer->pkid;
			}else{
				$cid = "12";
			}
			if($imei != ""){
				$this->db->select("t2.name,t1.code");
				$this->db->from("mobile_code_gift t1");
				$this->db->join("mobile_gift t2", "t1.gift_pkid=t2.pkid","left");
				$this->db->join("mobile_member_gift t3","t1.member_pkid=t3.pkid");
				$this->db->where("t3.imei",$imei);
				$this->db->where("t3.customer_pkid",$cid);
				$this->db->order_by("t1.pkid","desc");
				$res = $this->db->get()->result_array();
				foreach ($res as $P){
					$tmp['name']			=$P['name'];
					$tmp['code']			=$P['code'];
					$arrs[] = $tmp;
				}
			}
			echo json_encode($arrs);
		}
		
		public function member_type($imei,$str = ""){
			$this->db->select("t2.member_type");
			$this->db->from("mobile_member_gif t1");
			$this->db->join("mobile_customer t2", "t1.customer_pkid=t2.pkid");
			$this->db->where("t2.name",$str);
			$this->db->where("t1.imei",$imei);
			$rows = $this->db->get()->row();
			if($rows->member_type == 0){
				$arrs['StatusID']  	= "0";
				$arrs['Error']			="For Member VIP Only!";
			}else{
				$arrs['StatusID']  	= "1";
				$arrs['Error']			="";
			}
		}
		
		public function gift_list($str="",$type,$limit=""){
			$arr = array();
			if($str != ""){
				$this->db->from("mobile_customer t1");
				$this->db->join("mobile_gift t2", "t1.pkid = t2.customer_pkid");
				$this->db->where("t1.name",$str);
				$this->db->where("t2.gift_type",$type);
				$this->db->where("t2.status",1);
				$this->db->order_by("t2.pkid","desc");
				if($limit !=""){
					$this->db->limit($limit);
				}
				$res = $this->db->get()->result_array();
				foreach ($res as $index){
					$tmp['pkid']			=$index['pkid'];
					$tmp['name']			=$index['name'];
					//$tmp['content']		=stripslashes($index['content']);
					$tmp['img']			=$index['img'];
					$arr[]		=$index;
				}
			}
			echo json_encode($arr);
		}
		
		public function get_gift_other($str="",$imei="",$gift_id){
			//$imei = $this->input->post("imei");
			//$imei = "358867040228009";
			$arr = array();
			$this->db->where("name",$str);
			$rowid = $this->db->get("mobile_customer")->row();
			
			$this->db->select("pkid,name,point");
			$this->db->where("imei",$imei);
			$this->db->where("customer_pkid",$rowid->pkid);
			$rowmember =$this->db->get("mobile_member_gift")->row();
			$tmp['mname']			=$rowmember->name;
			$tmp['point']				=$rowmember->point;
			// get gift
			$this->db->where("status",1);
			$this->db->where("customer_pkid",$rowid->pkid);
			$this->db->where("pkid",$gift_id);
			//$this->db->order_by("pkid","desc");
			$rowsgift =$this->db->get("mobile_gift")->row();
			if(count($rowsgift) > 0){
				$tmp['gname']				=$rowsgift->name;
				$tmp['content']				=$rowsgift->content;
				$tmp['img']					=$rowsgift->img;
				// check code
				$this->db->where("member_pkid",$rowmember->pkid);
				$this->db->where("gift_pkid",$rowsgift->pkid);
				if($this->db->count_all_results("mobile_code_gift") > 0){
					$this->db->select("code");
					$this->db->where("member_pkid",$rowmember->pkid);
					$this->db->where("gift_pkid",$rowsgift->pkid);
					$rowc = $this->db->get("mobile_code_gift")->row();
					$tmp['status'] = 'n';
					$tmp['codegen'] = $rowc->code;
				}else{
					$tmp['status'] = 'y';
					$tmp['codegen']="";
				}
			}else{
				$tmp['gname']				="";
				$tmp['content']				="";
				$tmp['img']					="";
				$tmp['status'] ="";
				$tmp['codegen'] ="";
			}
				
			$arr[] = $tmp;
			echo json_encode($arr);
		}
		
		public function gift_gen_code($str=""){
			$rm			=$this->input->post("rm");
			$imei			=$this->input->post("imei");
			$gid			=$this->input->post("gid");
			
			$this->db->select("pkid");
			$this->db->where("name",$str);
			$rowid = $this->db->get("mobile_customer")->row();
			
			$this->db->select("pkid,gift_type,gift_number");
			$this->db->where("status",1);
			$this->db->where("pkid",$gid);
			$rowsgift =$this->db->get("mobile_gift")->row();
			
			//==== case other gift
			if($rowsgift->gift_type == 1){
					if($rm != "" && $imei != ""){
						$this->db->select("pkid");
						$this->db->where("imei",$imei);
						$this->db->where("customer_pkid",$rowid->pkid);
						$rowm = $this->db->get("mobile_member_gift")->row();
						$data = array(
								"member_pkid"=>$rowm->pkid,
								"gift_pkid"=>$rowsgift->pkid,
								"code"=>$rm,
								"created_date"=>date('Y-m-d H:i:s')
						);
						$res = $this->db->insert("mobile_code_gift",$data);
						if($res){
							$arrs['StatusID']  	= "1";
							$arrs['Error']			="Success";
						}else{
							$arrs['StatusID']  	= "0";
							$arrs['Error']			="Fail";
						}
					}else{
						$arrs['StatusID']  	= "0";
						$arrs['Error']			="Fail";
					}
			}
			///===
			else{
				$num_limit = $rowsgift->gift_number;
				$this->db->where("gift_pkid",$gid);
				if($num_limit > $this->db->count_all_results("mobile_code_gift")){
					if($rm != "" && $imei != ""){
						$this->db->select("pkid");
						$this->db->where("imei",$imei);
						$this->db->where("customer_pkid",$rowid->pkid);
						$rowm = $this->db->get("mobile_member_gift")->row();
						$data = array(
								"member_pkid"=>$rowm->pkid,
								"gift_pkid"=>$rowsgift->pkid,
								"code"=>$rm,
								"created_date"=>date('Y-m-d H:i:s')
						);
						$res = $this->db->insert("mobile_code_gift",$data);
						if($res){
							$arrs['StatusID']  	= "1";
							$arrs['Error']			="Success";
						}else{
							$arrs['StatusID']  	= "0";
							$arrs['Error']			="Fail";
						}
					}else{
						$arrs['StatusID']  	= "0";
						$arrs['Error']			="Fail";
					}
				}else{
					$arrs['StatusID']  	= "0";
					$arrs['Error']			="Limit Code gift";
				}
			}
			echo json_encode($arrs);
		}
		
	}
?>